import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
import _regeneratorRuntime from "@babel/runtime/regenerator";
import _asyncToGenerator from "@babel/runtime/helpers/esm/asyncToGenerator";
import request from 'utils/request';
import { CPAAS_IMP } from 'utils/config';
import { parseParameters, isTenantRoleLevel, getCurrentOrganizationId } from 'utils/utils';
/**
 * 模板头数据查询接口
 * @async
 * @function loadTemplate
 * @param {Object} params - 查询参数
 * @param {String} params.code - 模板编码
 */

export function loadTemplate(_x) {
  return _loadTemplate.apply(this, arguments);
}
/**
 * 导入数据查询
 * @async
 * @function loadDataSource
 * @param {Object} params - 模板编码等必输信息
 * @param {String} params.prefixPatch - 客户端路径前缀
 * @param {String} params.templateCode - 模板编码
 * @param {String} params.batch - 批次编码
 * @param {Object} query - 查询参数
 */

function _loadTemplate() {
  _loadTemplate = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee(params) {
    var prefixPatch, code, tenantId, site, reqUrl;
    return _regeneratorRuntime.wrap(function _callee$(_context) {
      while (1) {
        switch (_context.prev = _context.next) {
          case 0:
            prefixPatch = params.prefixPatch, code = params.code, tenantId = params.tenantId;
            site = isTenantRoleLevel();
            reqUrl = prefixPatch ? "".concat(prefixPatch, "/v1/").concat(tenantId, "/import/template/").concat(code, "/info") : "".concat(CPAAS_IMP, "/v1/").concat(site ? "".concat(tenantId, "/") : '', "template/").concat(code, "/info").concat(!site ? "?tenantId=".concat(getCurrentOrganizationId()) : '');
            return _context.abrupt("return", request(reqUrl, {
              method: 'GET'
            }));

          case 4:
          case "end":
            return _context.stop();
        }
      }
    }, _callee);
  }));
  return _loadTemplate.apply(this, arguments);
}

export function loadDataSource(_x2) {
  return _loadDataSource.apply(this, arguments);
}
/**
 * 导入数据验证
 * @async
 * @function validateData
 * @param {Object} params - 查询参数
 * @param {String} params.prefixPatch - 客户端路径前缀
 * @param {String} params.templateCode - 模板编码
 * @param {String} params.batch - 批次编码
 * @param {String} params.sync - 是否同步
 */

function _loadDataSource() {
  _loadDataSource = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee2(params) {
    var query,
        templateCode,
        batch,
        prefixPatch,
        tenantId,
        reqUrl,
        _args2 = arguments;
    return _regeneratorRuntime.wrap(function _callee2$(_context2) {
      while (1) {
        switch (_context2.prev = _context2.next) {
          case 0:
            query = _args2.length > 1 && _args2[1] !== undefined ? _args2[1] : {};
            templateCode = params.templateCode, batch = params.batch, prefixPatch = params.prefixPatch, tenantId = params.tenantId;
            reqUrl = "".concat(prefixPatch, "/v1/").concat(tenantId, "/import/data");
            return _context2.abrupt("return", request(reqUrl, {
              method: 'GET',
              query: _objectSpread({
                templateCode: templateCode,
                batch: batch
              }, parseParameters(query))
            }));

          case 4:
          case "end":
            return _context2.stop();
        }
      }
    }, _callee2);
  }));
  return _loadDataSource.apply(this, arguments);
}

export function validateData(_x3) {
  return _validateData.apply(this, arguments);
}
/**
 * 导入数据到正式库
 * @async
 * @function importData
 * @param {Object} params - 查询参数
 * @param {String} params.templateCode - 模板编码
 * @param {String} params.prefixPatch - 客户端路径前缀
 * @param {String} params.batch - 批次编码
 * @param {String} params.sync - 是否同步
 */

function _validateData() {
  _validateData = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee3(params) {
    var sync, templateCode, prefixPatch, batch, tenantId, reqUrl;
    return _regeneratorRuntime.wrap(function _callee3$(_context3) {
      while (1) {
        switch (_context3.prev = _context3.next) {
          case 0:
            sync = params.sync, templateCode = params.templateCode, prefixPatch = params.prefixPatch, batch = params.batch, tenantId = params.tenantId;
            reqUrl = sync ? "".concat(prefixPatch, "/v1/").concat(tenantId, "/import/data/sync/data-validate") : "".concat(prefixPatch, "/v1/").concat(tenantId, "/import/data/data-validate");
            return _context3.abrupt("return", request(reqUrl, {
              method: 'POST',
              query: {
                templateCode: templateCode,
                batch: batch
              }
            }));

          case 3:
          case "end":
            return _context3.stop();
        }
      }
    }, _callee3);
  }));
  return _validateData.apply(this, arguments);
}

export function importData(_x4) {
  return _importData.apply(this, arguments);
}
/**
 * 导入单条数据到正式库
 * @async
 * @function updateOne
 * @param {Object} params - 查询参数
 * @param {String} params.prefixPatch - 客户端路径前缀
 * @param {String} params.data - 单条保存数据
 * @param {String} params.id - 单条保存数据 id
 */

function _importData() {
  _importData = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee4(params) {
    var sync, prefixPatch, templateCode, batch, tenantId, reqUrl;
    return _regeneratorRuntime.wrap(function _callee4$(_context4) {
      while (1) {
        switch (_context4.prev = _context4.next) {
          case 0:
            sync = params.sync, prefixPatch = params.prefixPatch, templateCode = params.templateCode, batch = params.batch, tenantId = params.tenantId;
            reqUrl = sync ? "".concat(prefixPatch, "/v1/").concat(tenantId, "/import/data/sync/data-import") : "".concat(prefixPatch, "/v1/").concat(tenantId, "/import/data/data-import");
            return _context4.abrupt("return", request(reqUrl, {
              method: 'POST',
              query: {
                templateCode: templateCode,
                batch: batch
              }
            }));

          case 3:
          case "end":
            return _context4.stop();
        }
      }
    }, _callee4);
  }));
  return _importData.apply(this, arguments);
}

export function updateOne(_x5) {
  return _updateOne.apply(this, arguments);
}
/**
 * 删除单挑数据
 * @async
 * @function removeOne
 * @param {Object} params - 查询参数
 * @param {String} params.prefixPatch - 客户端路径前缀
 * @param {String} params.data - 单条保存数据
 * @param {String} params.id - 单条保存数据 id
 */

function _updateOne() {
  _updateOne = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee5(params) {
    var prefixPatch, id, data, tenantId, reqUrl;
    return _regeneratorRuntime.wrap(function _callee5$(_context5) {
      while (1) {
        switch (_context5.prev = _context5.next) {
          case 0:
            prefixPatch = params.prefixPatch, id = params._id, data = params.data, tenantId = params.tenantId;
            reqUrl = "".concat(prefixPatch, "/v1/").concat(tenantId, "/import/data/").concat(id);
            return _context5.abrupt("return", request(reqUrl, {
              method: 'PUT',
              body: data
            }));

          case 3:
          case "end":
            return _context5.stop();
        }
      }
    }, _callee5);
  }));
  return _updateOne.apply(this, arguments);
}

export function removeOne(_x6) {
  return _removeOne.apply(this, arguments);
}
/**
 * 状态查询
 * @async
 * @function queryStatus
 * @param {Object} params - 查询参数
 * @param {String} params.prefixPatch - 客户端路径前缀
 * @param {Object} query - 查询参数
 */

function _removeOne() {
  _removeOne = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee6(params) {
    var prefixPatch, id, tenantId, reqUrl;
    return _regeneratorRuntime.wrap(function _callee6$(_context6) {
      while (1) {
        switch (_context6.prev = _context6.next) {
          case 0:
            prefixPatch = params.prefixPatch, id = params._id, tenantId = params.tenantId;
            reqUrl = "".concat(prefixPatch, "/v1/").concat(tenantId, "/import/data/").concat(id);
            return _context6.abrupt("return", request(reqUrl, {
              method: 'DELETE'
            }));

          case 3:
          case "end":
            return _context6.stop();
        }
      }
    }, _callee6);
  }));
  return _removeOne.apply(this, arguments);
}

export function queryStatus(_x7) {
  return _queryStatus.apply(this, arguments);
}
/**
 * 查询当前模板 导入历史
 * @param {object} params
 * @param {string} params.prefixPatch
 * @param {string} params.templateCode
 * @param query
 * @return {Promise<void>}
 */

function _queryStatus() {
  _queryStatus = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee7(params) {
    var query,
        prefixPatch,
        tenantId,
        reqUrl,
        _args7 = arguments;
    return _regeneratorRuntime.wrap(function _callee7$(_context7) {
      while (1) {
        switch (_context7.prev = _context7.next) {
          case 0:
            query = _args7.length > 1 && _args7[1] !== undefined ? _args7[1] : {};
            prefixPatch = params.prefixPatch, tenantId = params.tenantId;
            reqUrl = "".concat(prefixPatch, "/v1/").concat(tenantId, "/import/data/status");
            return _context7.abrupt("return", request(reqUrl, {
              query: query
            }));

          case 4:
          case "end":
            return _context7.stop();
        }
      }
    }, _callee7);
  }));
  return _queryStatus.apply(this, arguments);
}

export function queryImportHistory(_x8) {
  return _queryImportHistory.apply(this, arguments);
}
/**
 * 删除 导入记录
 * @param {object} params
 * @param {object[]} records - 要删除的记录
 * @return {Promise<void>}
 */

function _queryImportHistory() {
  _queryImportHistory = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee8(params) {
    var query,
        prefixPatch,
        templateCode,
        tenantId,
        reqUrl,
        _args8 = arguments;
    return _regeneratorRuntime.wrap(function _callee8$(_context8) {
      while (1) {
        switch (_context8.prev = _context8.next) {
          case 0:
            query = _args8.length > 1 && _args8[1] !== undefined ? _args8[1] : {};
            prefixPatch = params.prefixPatch, templateCode = params.templateCode, tenantId = params.tenantId;
            reqUrl = "".concat(prefixPatch, "/v1/").concat(tenantId, "/import/manager");
            return _context8.abrupt("return", request(reqUrl, {
              query: _objectSpread(_objectSpread({}, parseParameters(query)), {}, {
                templateCode: templateCode
              }),
              method: 'GET'
            }));

          case 4:
          case "end":
            return _context8.stop();
        }
      }
    }, _callee8);
  }));
  return _queryImportHistory.apply(this, arguments);
}

export function deleteImportHistory(_x9, _x10) {
  return _deleteImportHistory.apply(this, arguments);
}

function _deleteImportHistory() {
  _deleteImportHistory = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee9(params, records) {
    var prefixPatch, tenantId, reqUrl;
    return _regeneratorRuntime.wrap(function _callee9$(_context9) {
      while (1) {
        switch (_context9.prev = _context9.next) {
          case 0:
            prefixPatch = params.prefixPatch, tenantId = params.tenantId;
            reqUrl = "".concat(prefixPatch, "/v1/").concat(tenantId, "/import/manager");
            return _context9.abrupt("return", request(reqUrl, {
              method: 'DELETE',
              body: records
            }));

          case 3:
          case "end":
            return _context9.stop();
        }
      }
    }, _callee9);
  }));
  return _deleteImportHistory.apply(this, arguments);
}